package com.qs.rocketmq.common.util;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import dto.PageDTO;

import java.util.List;
import java.util.stream.Collectors;

public class PageUtil {

    public static <E, D> PageDTO<D> wrapPageDTO(List<E> list, Class<D> dClass) {
        PageDTO<D> pageDTO = new PageDTO<>();
        PageInfo<E> pageInfo = new PageInfo<>(list);
        pageDTO.setPages(pageInfo.getPages());
        pageDTO.setPageNum(pageInfo.getPageNum());
        pageDTO.setPageSize(pageInfo.getPageSize());
        pageDTO.setTotal(pageInfo.getTotal());

        List<D> content = pageInfo.getList().stream().map(e -> {
            D d = null;
            try {
                d = dClass.getConstructor().newInstance();
                BeanUtil.copyProperties(e, d);
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            return d;
        }).collect(Collectors.toList());
        pageDTO.setContent(content);
        return pageDTO;
    }
}
